package br.ufrgs.inf.cwsmart.exception;


/**
 * A class that represents an error occurred during the
 * framework execution 
 * 
 * @author Rodrigo S Lumertz
 */
public class FrameworkException extends Exception
{
	/**
	 * A default serialVersionUID for serialization support. 
	 */
	private static final long serialVersionUID = 3108265411077059985L;
	
	
	/* 
	 * --------------------------------------------------------------------
	 *                                 Error Codes
	 * --------------------------------------------------------------------
	 */

	/**
	 * An enumeration of possible error codes.
	 */
	public static enum ErrorCode
	{			
		//MATCHER		
		UNKNOWN_MATCHER_ERROR("An match error of unknown type has occurred."),
		MART_PROPERTIES_FILE_NOT_FOUND("Configuration file config/mart.properties not found."),
		MART_PROPERTIES_FILE_PARSING_ERROR("An error has been found while file config/mart.properties was parsed."),
		MART_FUNCTIONALITIES_NOT_FOUND("Configuration file config/mart.properties does not contains the 'functionalities' property."),
		SEMANTIC_ERROR("An error has ocurred while the semantic comparisson was taking place."),		
		RULE_ERROR("An error has ocurred while the rules checking was taking place."),

		//DESCRIPTION
		PERSIST_ERROR("An error during the persist operation has been encountered"),
		PARSING_NOT_SUPPORTED_EXCEPTION("The parsing method throws a NotSupportedException."),
		
		//RECOMMENDATION
		
		//MINING
		
		//MANAGEMENT
		
		//MONITORING
		
		//DB MANAGERS
		DATABASE_ACCESS_EXCEPTION("An error during the access to the database has occurred."),
		REPOSITORY_DB_NOT_FOUND("Configuration file config/mart.config does not contains the 'mart_db' property."),
		DATAMART_DB_NOT_FOUND("Configuration file config/mart.config does not contains the 'mart_db' property."),
		MART_CONFIG_FILE_NOT_FOUND("Configuration file config/mart.config not found."),
		DB_CONNECTION_ERROR("An error has been found while a connection was being created."),
		DB_QUERY_EXECUTION_EROR("An error has been found while a script was being executed."),
		
		//ESPECIFIC FRAMEWORK EXCEPTIONS
		ILLEGAL_FRAMEWORK_ACCESS("It's mandatory instantiate the framework first!"),
		
		//GENERAL EXCEPTIONS CODES
		UNKNOWN_ERROR("An error of unknown type has occurred."),
		LANGUAGE_NOT_SUPPORTED("An unknown CWS language has been encountered."),		
		PARSING_ERROR("An error during the parsing operation has been encountered."),
		EXTENSIBLE_INSTANCE_NOT_FOUND("An null instance has been encountered when a extensible instance should be found.");
		
		/**
		 * The error message associated with this code. 
		 */
		private String message;
		
		
		/**
		 * Constructs a new ErrorCode with the given string as textual message.
		 * 
		 * @param message The message for the ErrorCode.
		 */
		private ErrorCode(String message) {
			this.message = message;
		}
		
		/**
		 * Returns the error message that corresponds to this ErrorCode.
		 * 
		 * @return The message for this ErrorCode.
		 */
		public String getErrorMessage() {
			return this.message;
		}
	}
	

	
	
	/* 
	 * --------------------------------------------------------------------
	 *                                  Exception
	 * --------------------------------------------------------------------
	 */
	
	/**
	 * The error code, describing the kind of error that arose.
	 */
	protected ErrorCode errorcode;
	
	
	/**
	 * Default constructor with no additional information.
	 */
	public FrameworkException() {		
		super();
		
		// we don't know anything
		this.errorcode = ErrorCode.UNKNOWN_ERROR;		
	}

	/**
	 * Create a FrameworkException for the given error.
	 * 
	 * @param code The ErrorCode that arose.
	 */
	public FrameworkException(ErrorCode code)
	{
		this.errorcode = code;
	}
	
	/**
	 * Returns the error message that corresponds to the given error code.
	 * 
	 * @param code The error code to translate to a message.
	 * @return The message for the given ErrorCode.
	 */
	public static String getErrorMessage(ErrorCode code)
	{
		return code.getErrorMessage();
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Throwable#getMessage()
	 */
	public String getMessage()
	{
		StringBuilder message = new StringBuilder(256);
		
		message.append("An error occurred during framework execution: " + errorcode.getErrorMessage());		
		
		return message.toString();
	}
		
	/**
	 * Gets the ErrorCode for this Exception.
	 * 
	 * @return The ErrorCode for this Exception
	 */
	public ErrorCode getErrorcode()
	{
		return errorcode;
	}	

	
	/* (non-Javadoc)
	 * @see java.lang.Throwable#toString()
	 */
	public String toString()
	{
		return "FrameworkError:\nErrorCode: " + errorcode.name() + "\n";
	}
}